package net.sevear.chaos.view.recommend
{
	
	import com.douban.auth.DBAuthenticate;
	import com.douban.event.AuthenticateEvent;
	import com.douban.media.DBRadio;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.events.FlexEvent;
	
	import net.sevear.chaos.douban.DBShuoManager;
	import net.sevear.chaos.event.FanRollEvent;
	
	import spark.components.Group;
	import spark.components.HGroup;
	import spark.components.Label;
	
	public class RecommendPanel extends Group
	{
		private var _topGroup:HGroup;
		private var _logInNameLbl:Label;
		private var _toLbl:Label;
		private var _users:UserGroup;
		private var _fan:FanRotator;
		private var _shuo:Label;
		private var _withShuo:Boolean = false;
		public function RecommendPanel(width:int,height:int)
		{
			super();
			this.width = width;
			this.height = height;
			
			_topGroup = new HGroup();
			_topGroup.width = this.width;
			_topGroup.height = 20;
			_topGroup.y = 0;
			_topGroup.x = 0;
			this.addElement(_topGroup);
			
			_logInNameLbl = new Label();
			_logInNameLbl.setStyle("fontSize",12);
			_logInNameLbl.addEventListener(FlexEvent.INITIALIZE,onNameLblInit);
			_topGroup.addElement(_logInNameLbl);
			
			_toLbl = new Label();
			_toLbl.setStyle("fontSize",12);
			_toLbl.setStyle("fontWeight","bold");
			_toLbl.text = "To:";
			_topGroup.addElement(_toLbl);
			
			//TODO:draw a dotted line as sepeate line
			
			_shuo = new Label();
			_shuo.right = 100;
			_shuo.bottom = 10;
			_shuo.text = "同时推荐到豆瓣说";
			_shuo.addEventListener(MouseEvent.CLICK,onClickShuo);
			this.addElement(_shuo);
			
			_fan = new FanRotator(80);
			_fan.right = 10;
			_fan.bottom = 10;
			this.addElement(_fan);
			_fan.addEventListener(FanRollEvent.ROLLING_EFFECT_END,onFanRollEnd);
			
			_users = new UserGroup(245);
			_users.x = 10;
			_users.y = 33;
			_users.addEventListener(UserGroup.USER_GROUP_SELECTED_CHANGE,onUidsSelectedChange);
			this.addElement(_users);
			_users.show();
			
			
		}
		
		private function onNameLblInit(e:FlexEvent):void{
			_logInNameLbl.removeEventListener(FlexEvent.INITIALIZE,onNameLblInit);
			DBAuthenticate.getInstance().addEventListener(AuthenticateEvent.GOTTEN_DOUBAN_INFO,onGetName);
		}
		
		private function onGetName(e:AuthenticateEvent):void{
			_logInNameLbl.text = DBAuthenticate.getInstance().dbName;
			_logInNameLbl.toolTip = DBAuthenticate.getInstance().dbUID.toString();
		}
		
		private function onUidsSelectedChange(e:Event):void{
			_fan.setSelectedIDs(_users.selectedUIDs);
		}
		
		private function onClickShuo(e:MouseEvent):void{
			if(_withShuo){
				_withShuo = false;
				_shuo.setStyle("color",0xb7babc);
				_shuo.setStyle("backgroundAlpha",0);
			}else{
				_withShuo = true;
				_shuo.setStyle("backgroundAlpha",1);
				_shuo.setStyle("backgroundColor",0xb7babc);
				_shuo.setStyle("color",0xffffff);
			}
		}
		
		private function onFanRollEnd(e:FanRollEvent):void{
			if(_withShuo){
				DBShuoManager.getInstance().recommend(DBRadio.getInstance().curSong,
					_users.selectedUsers);
			}
		}
	}
}